package com.dhcc.core.modules.msgcenter.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.msgcenter.cache.MsgCache;
import com.dhcc.core.modules.msgcenter.cache.IMsgCache;
import com.dhcc.core.modules.msgcenter.entity.MsgModule;
import com.dhcc.core.modules.msgcenter.service.IMsgModuleService;
import com.dhcc.core.modules.msgcenter.warpper.MsgModuleWarpper;


/**
 * 消息模块
 * 
 * @ClassName: MsgModuleController
 * @Description: TODO
 * @author cyf
 * @date 2018-11-15 15:02:17
 */
@Controller
@RequestMapping("${sys.backendPath}/msgcenter/module")
public class MsgModuleController extends BaseController {

	private String PREFIX = "/backend/msgcenter/module/";

	@Autowired
	private IMsgModuleService msgModuleService;
	

    /**
     * 跳转到消息模块列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("pushTypes", MsgCache.me().getPushTypes());
        return PREFIX + "module.html";
    }
	
    /**
     * 跳转到新增消息模块
     */
    @RequestMapping("/to_add")
    public String moduleAdd(Model model) {
        model.addAttribute("pushTypes", MsgCache.me().getPushTypes());
        return PREFIX + "module_add.html";
    }
	
	/**
     * 跳转到修改消息模块
     */
    @RequestMapping("/to_update/{moduleId}")
    public String moduleUpdate(@PathVariable Long moduleId, Model model) {
        MsgModule module = this.msgModuleService.selectById(moduleId);
        model.addAttribute("pushTypes", MsgCache.me().getPushTypes());
        model.addAttribute("module",module);
        return PREFIX + "module_edit.html";
    }

    /**
     * 获取消息模块列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<MsgModule> page = new PageFactory<MsgModule>().defaultPage();
        List<MsgModule> result = this.msgModuleService.page(page,params);
        page.setRecords((List<MsgModule>) new MsgModuleWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增消息模块
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(IMsgCache.MSG_CENTER_CACHE)
    public Object add(MsgModule module) {
        if (CommonUtil.isOneEmpty(module)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        module.insert();
        return R.ok();
    }

    /**
     * 删除消息模块
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(IMsgCache.MSG_CENTER_CACHE)
    public Object delete(@RequestParam Long moduleId) {
	
        this.msgModuleService.deleteById(moduleId);

        return R.ok();
    }

    /**
     * 修改消息模块
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @CacheClear(IMsgCache.MSG_CENTER_CACHE)
    public Object update(MsgModule module) {
        if (CommonUtil.isOneEmpty(module)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        MsgModule old = this.msgModuleService.selectById(module.getId());

        old.setModuleCode(module.getModuleCode());
        old.setModuleName(module.getModuleName());
        old.setModuleUrl(module.getModuleUrl());
        old.setPushTypes(module.getPushTypes());
        old.setTemplate(module.getTemplate());
        old.updateById();
        return R.ok();
    }
	
}
